package com.fbt.energy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbt.energy.dao.AmmeterDao;
import com.fbt.energy.entity.Ammeter;
import com.fbt.energy.entity.Equipment;
import com.fbt.energy.entity.ProductionLine;
import com.fbt.energy.entity.VO.EqVO;
import com.fbt.energy.entity.VO.PrVO;
import com.fbt.energy.service.AmmeterService;
import com.fbt.energy.service.EquipmentService;
import com.fbt.energy.service.ProductionLineService;
import com.fbt.energy.util.Arith;
import com.fbt.energy.util.GetNumberOfDays;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 电表设备表(Ammeter)表服务实现类
 *
 * @author makejava
 * @since 2021-12-12 19:05:42
 */
@Service("ammeterService")
public class AmmeterServiceImpl extends ServiceImpl<AmmeterDao, Ammeter> implements AmmeterService {
    @Resource
    private AmmeterDao ammeterDao;

    @Resource
    AmmeterService ammeterService;

    @Resource
    private AmmeterData ammeterData;

    @Resource
    private ProductionLineService prService;

    @Resource
    private EquipmentService eqService;


    /**
     * 新增数据
     *
     * @param ammeter 实例对象
     * @return 实例对象
     */
    @Override
    public Ammeter insert(Ammeter ammeter) {
        this.ammeterDao.insert(ammeter);

        return ammeter;
    }


    /**
     * 根据时间查询所有小时数据
     */
    @Override
    public List<PrVO> getAllMWH(String queerTime) {

        System.out.println("查询时间-----" + queerTime);

        List<PrVO> prVOList = new ArrayList<>();

        List<ProductionLine> prList = prService.list(null);

        for (ProductionLine pr : prList) {
            String productionNo = pr.getProductionNo();
            String productionLineName = pr.getProductionLineName();
            PrVO prVO = new PrVO();
            prVO.setProductionLineNo(productionNo);
            prVO.setProductionLineName(productionLineName);


            QueryWrapper<Ammeter> prAmmeterWrapper = new QueryWrapper<>();


            /**
             * 优化思路：将存入数据库的时间值缩减到小时位，加索引做精确匹配*/
            prAmmeterWrapper.eq("production_no", productionNo);
            prAmmeterWrapper.like("querv_time", queerTime);

            List<Ammeter> prWMHList = ammeterService.list(prAmmeterWrapper);
/**产线小时*/
            Map<String, Double> prSum = prWMHList.stream().collect(Collectors.groupingBy(Ammeter::getQuervTime, Collectors.summingDouble(Ammeter::getElectricQuantity)));


            Collection<Double> prWMHListQuantitySTR = prSum.values();
            List<Double> collect = prWMHListQuantitySTR.stream().collect(Collectors.toList());

            List<EqVO> equipmentVoList = new ArrayList<>();

            QueryWrapper<Equipment> eqWrapper = new QueryWrapper<>();
            eqWrapper.eq("production_line_no", productionNo);
            List<Equipment> equipmentList = eqService.list(eqWrapper);
            for (int i = 0; i < equipmentList.size(); i++) {

                ArrayList<Double> amMWH = new ArrayList(24);


                Equipment equipment = equipmentList.get(i);
                String equipmentNo = equipment.getEquipmentNo();
                String equipmentName = equipment.getEquipmentName();
                String ammeterNo = equipment.getAmmeterNo();

                EqVO eqVO = new EqVO();
                eqVO.setEquipmentName(equipmentName);
                eqVO.setEquipmentNo(equipmentNo);

                QueryWrapper<Ammeter> ammeterWrapper = new QueryWrapper<>();
                /**
                 * 优化思路：将存入数据库的时间值缩减到小时位，加索引做精确匹配*/
                ammeterWrapper.eq("ammeter_no", ammeterNo);
                ammeterWrapper.like("querv_time", queerTime);

                List<Ammeter> ammeterList = ammeterService.list(ammeterWrapper);

                for (int x = 0; x < ammeterList.size(); x++) {

                    Ammeter ammeter = ammeterList.get(x);
                    Double electricQuantity = ammeter.getElectricQuantity();
                    amMWH.add(electricQuantity);

                }
                eqVO.setAmMWH(amMWH);

                equipmentVoList.add(eqVO);
            }

            prVO.setPrMWH(collect);
            prVO.setEqVOList(equipmentVoList);
            prVOList.add(prVO);
        }


        return prVOList;
    }


    /**
     * 根据时间查询设备，产线每天用电量
     */
    @Override
    // @Cacheable(key = "'getAllMWH'", value = "queerTime")

    public List<PrVO> getDayElectricity(String queerTime) {
        System.out.println("这个月的每一天用电量  " + queerTime);
        /**传输过来的格式是：2022-01*/

        List<PrVO> prVOList = new ArrayList<>();

        List<ProductionLine> prList = prService.list(null);

        int year = Integer.parseInt(queerTime.substring(0, 4));
        int month = Integer.parseInt(queerTime.substring(5, 7));

        List<String> dayByMonth = GetNumberOfDays.getDayByMonth(year, month);
        System.out.println("缩减过后的时间 " + dayByMonth);
        for (ProductionLine pr : prList) {

            String productionNo = pr.getProductionNo();
            String productionLineName = pr.getProductionLineName();
            PrVO prVO = new PrVO();
            List<Double> dayPrList = new ArrayList<>(31);
            prVO.setProductionLineNo(productionNo);
            prVO.setProductionLineName(productionLineName);

            for (String day : dayByMonth) {

                /**
                 * 优化思路
                 * 生成的时间做key，将查询出来的数据的时间的前几位截取出来，和循环的时间做比较，相等则做sum，并将值放到对应的value处
                 * 将like换做每月*/
                QueryWrapper<Ammeter> prAmmeterWrapper = new QueryWrapper<>();
                prAmmeterWrapper.eq("production_no", productionNo);
                prAmmeterWrapper.like("querv_time", day);

                List<Ammeter> prWMHList = ammeterService.list(prAmmeterWrapper);
                DoubleSummaryStatistics collect = prWMHList.stream().collect(Collectors.summarizingDouble(Ammeter::getElectricQuantity));
                double sum = collect.getSum();
                dayPrList.add(Arith.randomDouble(sum));
            }
            /**设备每天*/
            List<EqVO> equipmentVoList = new ArrayList<>();

            QueryWrapper<Equipment> eqWrapper = new QueryWrapper<>();
            eqWrapper.eq("production_line_no", productionNo);
            List<Equipment> equipmentList = eqService.list(eqWrapper);
            for (Equipment equipment : equipmentList) {

                List<Double> eqDayList = new ArrayList<>(31);

                String equipmentNo = equipment.getEquipmentNo();
                String equipmentName = equipment.getEquipmentName();
                String ammeterNo = equipment.getAmmeterNo();

                EqVO eqVO = new EqVO();
                eqVO.setEquipmentName(equipmentName);
                eqVO.setEquipmentNo(equipmentNo);

                for (String eqDay : dayByMonth) {
                    QueryWrapper<Ammeter> ammeterWrapper = new QueryWrapper<>();
                    /**
                     * 优化思路
                     * 生成的时间做key，将查询出来的数据的时间的前几位截取出来，和循环的时间做比较，相等则做sum，并将值放到对应的value处
                     * 将like换做每月*/
                    ammeterWrapper.eq("ammeter_no", ammeterNo);
                    ammeterWrapper.like("querv_time", eqDay);

                    List<Ammeter> ammeterList = ammeterService.list(ammeterWrapper);
                    DoubleSummaryStatistics eqSumDay = ammeterList.stream().collect(Collectors.summarizingDouble(Ammeter::getElectricQuantity));
                    double sum = eqSumDay.getSum();

                    eqDayList.add(Arith.randomDouble(sum));
                }
                eqVO.setAmMWH(eqDayList);
                equipmentVoList.add(eqVO);
            }
            prVO.setEqVOList(equipmentVoList);
            //    prVO.setPrMWH(dayMap);

            prVO.setPrMWH(dayPrList);
            prVOList.add(prVO);
        }
        long end = System.currentTimeMillis();

        return prVOList;
        // 单位为毫秒   long end = System.currentTimeMillis();
    }

    /**
     * 查询每月的数据
     */
    @Override
    public List<PrVO> getYearElectricity(String queerTime) {
        System.out.println("查询每月的数据   " + queerTime);
        String[] yearFullMonth = GetNumberOfDays.getYearFullMonth(queerTime);
        List<String> monthList = Arrays.stream(yearFullMonth).collect(Collectors.toList());

        List<PrVO> prVOList = new ArrayList<>();

        List<ProductionLine> prList = prService.list(null);
        for (ProductionLine pr : prList) {
            String productionNo = pr.getProductionNo();
            String productionLineName = pr.getProductionLineName();

            PrVO prVO = new PrVO();
            prVO.setProductionLineNo(productionNo);
            prVO.setProductionLineName(productionLineName);

            List<Double> yearPrList = new ArrayList<>();
            List<Map<String, Double>> peakPingguList = new ArrayList<>();


            for (String month : monthList) {

                QueryWrapper<Ammeter> prAmmeterWrapper = new QueryWrapper<>();
                Map<String, Double> peakPinggu = new HashMap<>();
                /**
                 * 优化思路
                 * 生成的时间做key，将查询出来的数据的时间的前几位截取出来，和循环的时间做比较，相等则做sum，并将值放到对应的value处
                 * */
                prAmmeterWrapper.eq("production_no", productionNo);
                prAmmeterWrapper.like("querv_time", month);

                List<Ammeter> prWMHList = ammeterService.list(prAmmeterWrapper);
                DoubleSummaryStatistics Electric = prWMHList.stream().collect(Collectors.summarizingDouble(Ammeter::getElectricQuantity));
                System.out.println("Electric   " + Electric);
                double sum = Electric.getSum();
                System.out.println("sum  " + sum);


                DoubleSummaryStatistics valleyPeriod = prWMHList.stream().collect(Collectors.summarizingDouble(Ammeter::getPeacetimePeriod));
                DoubleSummaryStatistics peakHour = prWMHList.stream().collect(Collectors.summarizingDouble(Ammeter::getPeakHour));
                DoubleSummaryStatistics peakPeriod = prWMHList.stream().collect(Collectors.summarizingDouble(Ammeter::getPeakPeriod));
                DoubleSummaryStatistics peacetimePeriod = prWMHList.stream().collect(Collectors.summarizingDouble(Ammeter::getPeakPeriod));
                double peakHourSum = peakHour.getSum();
                double peakPeriodSum = peakPeriod.getSum();
                double peacetimePeriodSum = peacetimePeriod.getSum();
                double valleyPeriodSum = valleyPeriod.getSum();
                System.out.println("peakHourSum  >" + peakHourSum + "  peakPeriodSum  >" + peakPeriodSum + "  peacetimePeriodSum  >" + peacetimePeriodSum + "valleyPeriodSum   >" + valleyPeriodSum);
                peakPinggu.put("尖", Arith.randomDouble(peakHourSum));
                peakPinggu.put("峰", Arith.randomDouble(peakPeriodSum));
                peakPinggu.put("平", Arith.randomDouble(peacetimePeriodSum));
                peakPinggu.put("谷", Arith.randomDouble(valleyPeriodSum));
                peakPingguList.add(peakPinggu);
                yearPrList.add(Arith.randomDouble(sum));
            }
            /**设备每月*/
            List<EqVO> equipmentVoList = new ArrayList<>();

            QueryWrapper<Equipment> eqWrapper = new QueryWrapper<>();
            eqWrapper.eq("production_line_no", productionNo);
            List<Equipment> equipmentList = eqService.list(eqWrapper);
            for (Equipment equipment : equipmentList) {

                List<Double> yearEqList = new ArrayList<>();
                List<Map<String,Double>> eqPeakPingguList=new ArrayList<>();

                String equipmentNo = equipment.getEquipmentNo();
                String equipmentName = equipment.getEquipmentName();
                String ammeterNo = equipment.getAmmeterNo();

                EqVO eqVO = new EqVO();
                eqVO.setEquipmentName(equipmentName);
                eqVO.setEquipmentNo(equipmentNo);

                for (String month : monthList) {
                    QueryWrapper<Ammeter> ammeterWrapper = new QueryWrapper<>();
                    ammeterWrapper.eq("ammeter_no", ammeterNo);
                    ammeterWrapper.like("querv_time", month);

                    Map<String, Double> eqPeakPinggu = new HashMap<>();

                    List<Ammeter> ammeterList = ammeterService.list(ammeterWrapper);
                    DoubleSummaryStatistics eqSumDay = ammeterList.stream().collect(Collectors.summarizingDouble(Ammeter::getElectricQuantity));

                    DoubleSummaryStatistics peakHour = ammeterList.stream().collect(Collectors.summarizingDouble(Ammeter::getPeakHour));
                    DoubleSummaryStatistics peakPeriod = ammeterList.stream().collect(Collectors.summarizingDouble(Ammeter::getPeakPeriod));
                    DoubleSummaryStatistics peacetimePeriod = ammeterList.stream().collect(Collectors.summarizingDouble(Ammeter::getPeacetimePeriod));
                    DoubleSummaryStatistics valleyPeriod = ammeterList.stream().collect(Collectors.summarizingDouble(Ammeter::getValleyPeriod));

                    double peakHourSum = peakHour.getSum();
                    double peakPeriodSum = peakPeriod.getSum();
                    double peacetimePeriodSum = peacetimePeriod.getSum();
                    double valleyPeriodSum = valleyPeriod.getSum();

                    eqPeakPinggu.put("eq尖", Arith.randomDouble(peakHourSum));
                    eqPeakPinggu.put("eq峰", Arith.randomDouble(peakPeriodSum));
                    eqPeakPinggu.put("eq平", Arith.randomDouble(peacetimePeriodSum));
                    eqPeakPinggu.put("eq谷", Arith.randomDouble(valleyPeriodSum));
                    double sum = eqSumDay.getSum();


                    yearEqList.add(Arith.randomDouble(sum));
                    eqVO.setAmMWH(yearEqList);
                    eqVO.setEqPeakPingguList(eqPeakPingguList);
                    eqPeakPingguList.add(eqPeakPinggu);
                }
                equipmentVoList.add(eqVO);
            }

            prVO.setEqVOList(equipmentVoList);
            //   prVO.setPrMWH(yearMap);
            prVO.setPrMWH(yearPrList);
            prVO.setPeakPingguList(peakPingguList);
            prVOList.add(prVO);
        }
        return prVOList;
    }


}